package com.bci.pwtz.service.impl;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.xerces.impl.dv.util.Base64;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.service.PictureUploadAndDownloadService;
@Service
public class PictureUploadAndDownloadServiceImpl implements PictureUploadAndDownloadService
{

    @Override
    public boolean upload(String remotePath,MultipartFile picfile,String filename) throws PwtzDatabaseException
    {
        FileOutputStream os=null;
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        File file=new File(remotePath+filename);
        if(!file.exists()&&!file.isDirectory()){
            file.mkdirs();
        }
        try
        {
            bis=new BufferedInputStream(picfile.getInputStream());
            os=new FileOutputStream(file);
            bos=new BufferedOutputStream(os);
            Streams.copy(bis, bos, true);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
            return false;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return false;
        }finally{
            if(bis!=null){
                try
                {
                    bis.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                    return false;
                }
            }
            if(os!=null){
                try
                {
                    os.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                    return false;
                }
            }
            if(bos!=null){
                try{
                    bos.close(); 
                }catch(IOException e){
                    e.printStackTrace();
                    return false;
                }
            }
        }
        return true;
    }

    @Override
    public String download(String fileurl,String filename) throws PwtzDatabaseException
    {
        String filepath=fileurl+filename;
        File file=new File(filepath);
        if(file.exists()){
            byte [] bytes=new byte[1024];
            int len=0;
            FileInputStream is=null;
            ByteArrayOutputStream bos=new ByteArrayOutputStream();
            try
            {
                is=new FileInputStream(file);
                while((len=is.read(bytes))!=-1){
                    bos.write(bytes, 0, len);
                }
                if(bos.size()>0){
                  byte[] buffer =bos.toByteArray();
                  String outputstring=Base64.encode(buffer);
                  return outputstring;
                }
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }finally{
                if(is!=null){
                    try
                    {
                        is.close();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                        return null;
                    }
                }
                if(bos!=null){
                    try
                    {
                        bos.close();
                    }
                    catch (IOException e)
                    {
                        e.printStackTrace();
                        return null;
                    }
                }
            }
        }
        return null;
    }

    @Override
    public boolean delete(String fileurl, String filename) throws PwtzDatabaseException
    {
        boolean result=false;
        File file=new File(fileurl+filename.substring(filename.indexOf("_")+1));
        if(file.exists()){
            result=file.delete();
        }else{
            result=true;
        }
        return result;
    }

}
